package service

import (
	"context"
	"fmt"
	__ "user-srv/basic/proto"
	"user-srv/handler/model"
)

type Server struct {
	__.UnimplementedUserServer
}

func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {
	var user model.User
	user = model.User{
		Mobile:   in.Mobile,
		Password: in.Password,
		Name:     in.Name,
		Status:   0,
	}
	err := user.CreateUser()
	if err != nil {
		return nil, err
	}

	return &__.CreateUserResp{UserId: int64(user.Id)}, nil
}

func (s *Server) LoginUser(_ context.Context, in *__.LoginUserReq) (*__.LoginUserResp, error) {

	var user model.User

	user.Mobile = in.Mobile

	err := user.GetUser()
	if err != nil {
		return nil, err
	}

	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}

	return &__.LoginUserResp{}, nil
}

func (s *Server) AddComment(_ context.Context, in *__.LoginUserReq) (*__.LoginUserResp, error) {

	var user model.User

	user.Mobile = in.Mobile

	err := user.GetUser()
	if err != nil {
		return nil, err
	}

	if user.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}

	return &__.LoginUserResp{}, nil
}
